package com.cloud.system.api.mapper;

import com.cloud.system.api.bean.NoticeBean;
import com.cloud.system.api.bean.NoticeCriteriaBean;
import com.cloud.system.api.entity.Notice;
import com.cloud.system.api.provider.NoticePageProvider;
import com.cloud.system.common.result.SelectItem;
import com.cloud.system.common.utils.BaseMapperHelper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface NoticeMapper extends BaseMapperHelper<Notice> {

    @SelectProvider(type = NoticePageProvider.class, method = "loadReceiveList")
    @Results({@Result(property = "noticeId", column = "notice_id", javaType = Integer.class),
            @Result(property = "title", column = "title", javaType = String.class),
            @Result(property = "content", column = "content", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "publishUnit", column = "publish_unit", javaType = String.class),
            @Result(property = "grade", column = "grade", javaType = Integer.class),
            @Result(property = "attachmentIds", column = "attachment_ids", javaType = String.class),
            @Result(property = "attachmentNames", column = "attachment_names", javaType = String.class),
            @Result(property = "isRead", column = "is_read", javaType = Integer.class)})
    List<NoticeBean> loadReceiveList(NoticeCriteriaBean noticeCriteriaBean);

    @SelectProvider(type = NoticePageProvider.class, method = "loadPublishList")
    @Results({@Result(property = "noticeId", column = "notice_id", javaType = Integer.class),
            @Result(property = "title", column = "title", javaType = String.class),
            @Result(property = "content", column = "content", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "publishUnit", column = "publish_unit", javaType = String.class),
            @Result(property = "grade", column = "grade", javaType = Integer.class),
            @Result(property = "attachmentIds", column = "attachment_ids", javaType = String.class),
            @Result(property = "attachmentNames", column = "attachment_names", javaType = String.class),})
    List<Notice> loadPublishList(NoticeCriteriaBean noticeCriteriaBean);

    @Select("select role_id value, role_name label from role_info")
    @Results({@Result(property = "value", column = "value", javaType = Integer.class),
            @Result(property = "label", column = "label", javaType = String.class)})
    List<SelectItem> loadRoleData();

    @Insert("insert into notice(title,content,create_time,create_human,create_human_id,publish_unit,grade,attachment_ids,attachment_names) values(#{title},#{content},#{createTime},#{createHuman},#{createHumanId},#{publishUnit},#{grade}," +
            "#{attachmentIds},#{attachmentNames})")
    @Options(useGeneratedKeys = true, keyProperty = "noticeId")
    Integer insertNotice(NoticeBean noticeBean);

    @Select("select * from notice where notice_id in (select notice_id from notice_send_role where role_id = #{roleId}) order by create_time desc limit 3")
    @Results({@Result(property = "noticeId", column = "notice_id", javaType = Integer.class),
            @Result(property = "title", column = "title", javaType = String.class),
            @Result(property = "content", column = "content", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "publishUnit", column = "publish_unit", javaType = String.class),
            @Result(property = "grade", column = "grade", javaType = Integer.class),
            @Result(property = "attachmentIds", column = "attachment_ids", javaType = String.class),
            @Result(property = "attachmentNames", column = "attachment_names", javaType = String.class),})
    List<Notice> loadNotice(Long roleId);

}
